//
//  ViewController.m
//  计算器
//
//  Created by Fenhua on 17/2/8.
//  Copyright © 2017年 huangfeng. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (retain,nonatomic)UIButton *button;
@property (retain,nonatomic)UILabel *lab;
@property (retain,nonatomic)NSMutableString *string;
@property (assign,nonatomic)double num1,num2;
@property (strong,nonatomic)NSString *str;

- (void)go:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.lab = [[UILabel alloc]initWithFrame:CGRectMake(70, 90, 200, 50)];
    [self.view addSubview:_lab];
    self.lab.layer.borderWidth = 1;
    self.lab.textColor = [UIColor blackColor];
    self.lab.layer.cornerRadius = 15;
    self.lab.textAlignment = NSTextAlignmentCenter;
    self.lab.font = [UIFont systemFontOfSize:32];
    
    NSArray *arry = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    int n = 0;
    int k = 3;
    for (int i=0; i<k; i++) {
        for (int j=0; j<k; j++) {
            self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            self.button.frame = CGRectMake(30+65*j, 150+65*i, 60, 60);
            self.button.layer.borderWidth = 1;
            self.button.layer.cornerRadius = 15;
            [self.button setTitle:[arry objectAtIndex:n++] forState:UIControlStateNormal];
            [self.view addSubview:_button];
            [self.button addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
    
    UIButton *button0 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button0.layer.borderWidth = 1.0;
    button0.layer.cornerRadius = 15;
    [button0 setFrame:CGRectMake(30, 345, 60, 60)];
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    [button0 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button0];
    
    NSArray *arry1 = [NSArray arrayWithObjects:@"+",@"-",@"*",@"/", nil];
    for (int i =0; i<4; i++) {
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button1.layer.borderWidth = 1;
        button1.layer.cornerRadius = 15;
        [button1 setFrame:CGRectMake(225, 150+65*i, 60, 60)];
        [button1 setTitle:[arry1 objectAtIndex:i] forState:UIControlStateNormal];
        [self.view addSubview:button1];
        [button1 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.layer.borderWidth = 1;
    button2.layer.cornerRadius = 15;
    [button2 setFrame:CGRectMake(160, 410, 125, 35)];
    [button2 setTitle:@"=" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button3.layer.borderWidth = 1;
    button3.layer.cornerRadius = 15;
    button3.frame = CGRectMake(30, 410, 125, 35);
    [button3 setTitle:@"AC" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(clean:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button4.layer.borderWidth = 1;
    button4.layer.cornerRadius = 15;
    button4.frame = CGRectMake(95, 345, 60, 60);
    [button4 setTitle:@"." forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(shuzi:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button5.layer.borderWidth = 1;
    button5.layer.cornerRadius = 15;
    [button5 setTitle:@"back" forState:UIControlStateNormal];
    button5.frame = CGRectMake(160, 345, 60, 60);
    [button5 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button5];
    
    self.string = [[NSMutableString alloc]init];
    self.str = [[NSString alloc]init];
}

- (void)back:(id)sender
{
    if (![self.lab.text isEqualToString:@""]) {
        [self.string deleteCharactersInRange:NSMakeRange([self.string length]-1,1)];
        self.lab.text = _string;
    }
}
- (void)clean:(id)sender
{
    [self.string setString:@""];
    self.num1 = 0;
    self.num2 = 0;
    self.lab.text = @"0";
}

- (void)shuzi:(id)sender
{
    [self.string appendString:[sender currentTitle]];
    self.lab.text = [NSString stringWithString:_string];
    self.num1 = [self.lab.text doubleValue];
    NSLog(@"self.num1 is %f",self.num1);
}

- (void)go:(id)sender
{
    if ([self.str isEqualToString:@""]) {
        self.num2 = self.num1;
        NSLog(@"%f",self.num2);
        self.lab.text = [NSString stringWithString:_string];
        [self.string setString:@""];
        self.str = [sender currentTitle];//保存运算符
        NSLog(@"%@",_str);
        /*[self.string appendString:self.str];
        self.lab.text = [NSString stringWithString:_string];
         [self.string setString:@""];
        */
    }
    else
    {
        if ([self.str isEqualToString:@"+"])
        {
            [self.string setString:@""];
            self.num2+=self.num1;
            self.num1 = self.num2;
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                self.lab.text = [NSString stringWithFormat:@"%f",self.num1];
                self.str = @"";
                if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"]) {
                    self.lab.text = [NSString stringWithFormat:@"%f",self.num1];
                    self.str = [sender currentTitle];
                }
            }
            else if ([[sender currentTitle]isEqualToString:@"+"]||[[sender currentTitle]isEqualToString:@"-"]||[[sender currentTitle]isEqualToString:@"*"]||[[sender currentTitle]isEqualToString:@"/"])
            {
                self.lab.text = [NSString stringWithFormat:@"%f",self.num1];
//                [self.string setString:@""];
//                self.str = [sender currentTitle];
//                NSLog(@"%@",_str);
//                [self.string appendString:self.str];
//                [self.string setString:@""];
                self.str = [sender currentTitle];
            }
        }
        else if([self.str isEqualToString:@"-"])
        {
            [self.string setString:@""];
            self.num2-=self.num1;
            self.num1 = self.num2;
            self.str = @"";
            if ([[sender currentTitle]isEqualToString:@"="]) {
                self.lab.text = [NSString stringWithFormat:@"%f",self.num1];
                self.str = @"";
            }
        }
        else if([self.str hasPrefix:@"*"])
        {
            [self.string setString:@""];
            self.num2*=self.num1;
            self.num1 = self.num2;
            self.str = @"";
            if([[sender currentTitle]isEqualToString:@"="])
            {
                self.lab.text = [NSString stringWithFormat:@"%f",self.num1];
                self.str = @"";
            }
        }
        else if ([self.str isEqualToString:@"/"])//之前的符号是/
        {
            [self.string setString:@""];//字符串清零
            self.num2/=self.num1;
            self.num1 = self.num2;
            self.str = @"";
            //判断输出上次结果后判断这次输入的是何符号
            if ([[sender currentTitle]isEqualToString:@"="])
            {
                NSLog(@"self.num2  is  %f",self.num2);
                self.lab.text=[NSString stringWithFormat:@"%f",self.num1];
                self.str=@"";
            }
        }
    }
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
